package cn.jzq.mysoul.yourname.config;

import cn.jzq.mysoul.yourname.util.DateUtil;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * 自动填充公共字段
 * @author 蒋正强
 */
@Component
public class MyMetaObjectHandler extends MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        setData(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setData(metaObject);
    }

    private void setData(MetaObject metaObject) {
        final String nowTime = DateUtil.getNowDateString("yyyy-MM-dd HH:dd:ss");

        Object createDate = getFieldValByName("createDate", metaObject);//mybatis-plus版本2.0.9+
        Object modifyDate = getFieldValByName("modifyDate", metaObject);
        if (createDate == null) {
            setFieldValByName("createDate", nowTime, metaObject);//mybatis-plus版本2.0.9+
        }

        if (modifyDate == null) {
            setFieldValByName("modifyDate", nowTime, metaObject);
        }
    }
}
